﻿var compare;
Chenxin.UI.KeyManage = Chenxin.extend(Chenxin.UI.PageCommon, {
    autoInitForm: false,
    actionUrl: 'service/keyAddHandler.ashx',
    createGrid: function() {
        return new Chenxin.UI.Pagination({
            actionUrl: this.actionUrl + '?op=getlist',
            domId: 'page',
            renderTo: 'key_table'
        });
    },
    loadkeyinfo: function() {
        var _this = this;
        Chenxin.ajax({
            type: "get",
            dataType: "text",
            paramType: 'json',
            url: this.actionUrl,
            data: { op: 'getkeyuseinfo', keyuseid: this.currentId },
            onSuccess: function(data, msg) {
                if (data.houseinfo) {
                    _this.keyuseinfo = data.houseinfo;
                }
                _this.renderKeyUseInfo();
                _this.createKeyUseList(this.currentId);
            }
        });
    },
    loadinfo: function(id) {
        var _this = this;
        Chenxin.ajax({
            type: "post",
            dataType: "text",
            paramType: 'json',
            async: false,
            url: this.actionUrl,
            data: { op: 'getmodel', modelid: id },
            onSuccess: function(data, msg) {
                if (data.model) {
                    _this.keyusemodel = data.model;
                }
            }
        });
    },
    loadKeyReceiptionInfo: function(id) {
        var _this = this;
        Chenxin.ajax({
            type: "post",
            dataType: "text",
            paramType: 'json',
            async: false,
            url: this.actionUrl,
            data: { op: 'getkeyreceiptionmodel', modelid: _this.oprId },
            onSuccess: function(data, msg) {
                if (data.model) {
                    _this.KeyReceiptionInfoModel = data.model;
                }
            }
        });
    },
    cancle: function() {
    },
    renderKeyUseInfo: function() {
        var m = this.keyuseinfo;
        var infoStr = '';
        infoStr += '<tr id="tr-house-info"><td height="81" colspan="11" class="gendan_box" >';
        infoStr += '<div class="gdtab"><div class="outborder">';
        infoStr += '<div class="infos intitss"><span class="rbpsn"><b>操作</b></span><span class="rbpsn"><b>状态</b></span><span class="rbpsn"><b>使用人</b></span><span class="rbtim"><b>时间</b></span><span class="key_icon fl"><input name="btn_usekey" type="button" class="btn_pon_2" value="使用记录添加"/></span><span class="key_icon fl"><input id="btn_key_search" type="button" class="btn_pon_2" value="查看钥匙"/></span><span class="key_icon fl"><input id="btn_key_edit" type="button" class="btn_pon_2" value="修改钥匙"/></span></div>';
        //infoStr += '<div class="infos intitss"><span class="rbpsn"><b>操作</b></span><span class="rbpsn"><b>状态</b></span><span class="rbpsn"><b>使用人</b></span><span class="rbtim"><b>时间</b></span><span class="key_icon fl"><input name="btn_usekey" type="button" class="btn_pon_2" value="使用记录添加"/></span><span class="key_icon fl"><input id="btn_key_search" type="button" class="btn_pon_2" value="查看钥匙"/></span><span class="key_icon fl"><input id="btn_key_edit" type="button" class="btn_pon_2" value="修改钥匙"/></span><span class="key_icon fl"><input id="btn_key_del" type="button" class="btn_pon_2" value="删除钥匙"/></span></div>';
        infoStr += '<div id="keyuse_table"></div>';
        infoStr += '</div><div id="keyuselist_page"></div></div></td</tr>';
        $('#' + this.currentId).after(infoStr);
    },
    createKeyUseList: function(count) {
        if (!this.keyUsePage) {
            this.keyUsePage = new Chenxin.UI.Pagination({
                domId: 'keyuselist_page',
                pageSize: 3,
                actionUrl: this.actionUrl + '?op=getkeyuselist',
                data: { keyid: this.currentId },
                render: function(data) {
                    document.getElementById('keyuse_table').innerHTML = data;
                }
            });
        } else {
            this.keyUsePage.recordCount = 0;
            this.keyUsePage.hasCount = false;
            this.keyUsePage.reload({ keyid: this.currentId });
        }
    },
    showKeyWin: function(op) {
        var _this = this;
        if (!this.keyWin) {
            this.keyWin = new Chenxin.UI.KeyForm({
                currentId: this.currentId,
                alwaysCheckPermit: true,
                afterGetFormJson: function(json) {
                    if (_this.keyWin.op == 'edit') {
                        json.ReceiptionId = _this.KeyReceiptionInfoModel.ReceiptionId;
                        delete json.AddPerson;
                        delete json.AddDepart;
                        json.HouseAddress = json.Address;
                        delete json.Address;
                        json.HouseId = _this.keyuseinfo.HouseId;
                    }
                }
            });
        }

        var _data = {};
        
        if (_this.keyWin.show(op, { currentId: _this.currentId })) {
            if (op == 'view') {
                _this.keyWin.op = 'view';
                title = '钥匙信息查看';
                _this.keyWin.setDisable();
            } else {
                _this.keyWin.op = 'edit';
                title = '钥匙信息编辑';
                _this.keyWin.setEnable();
            }
            //_this.keyWin.permitParam = { currentId: _this.currentId };

            _this.loadKeyReceiptionInfo();
            _data.HouseCode = this.keyuseinfo.HouseCode;
            _data.AddDepart = this.keyuseinfo.OprDepart;
            _data.AddPerson = this.keyuseinfo.OprPerson;
            _data.ReceiptionCode = _this.KeyReceiptionInfoModel.ReceiptionCode;
            _data.KeyCount = _this.KeyReceiptionInfoModel.KeyCount;
            var temp = _this.KeyReceiptionInfoModel.GetKeyDay.split(' ');
            _data.GetKeyDay = temp[0];
            _data.Address = _this.KeyReceiptionInfoModel.HouseAddress;
            _this.keyWin.fitData(_data);
            this.keyWin.setTitle('钥匙收据登记');
            this.keyWin.currentId = this.currentId;
        }
    },
    initEvent: function() {
        var _this = this;
        $('.tr_houselist').live('click', function() {

            $('.tr_houselist,.selected_orgbgcolor').each(function() {
                $(this).removeClass('selected_orgbgcolor');
            });
            $(this).toggleClass('selected_orgbgcolor');
            if (!$('#tr-house-info').length) {
                _this.currentId = this.id;
                _this.oprId = this.id;
                _this.loadkeyinfo();

            } else {
                $('#tr-house-info').remove();
                if (_this.currentId != this.id) {
                    _this.currentId = this.id;
                    _this.oprId = this.id;
                    _this.loadkeyinfo();
                }
            }
        });
        $("input[name='btn_usekey']").live('click', function() {
            _this.showKeyUseWin();
        });
        $("#btn_key_edit").live('click', function() {
            _this.op = 'edit';
            _this.showKeyWin('edit');
        });
        $("#btn_key_del").live('click', function() {
            _this.currentId = _this.oprId;
            _this.del('delkeyreceiption');
        });
        $("#btn_key_search").live('click', function() {
            _this.showKeyWin('view');
        });
        $(".tr_editKeyInfo").live('click', function() {
            _this.op = 'editkeyuse';
            this.currentId = this.name;
            _this.showKeyUseWinInfo(this.name, false);
        });
        $(".tr_delKeyInfo").live('click', function() {
            _this.currentId = this.name;
            _this.del();
        });
        $(".tr_searchKeyInfo").live('click', function() {
            _this.op = 'view';
            _this.showKeyUseWinInfo(this.name, true);
        });
    },
    /**
    *@description 钥匙管理
    */
    showKeyUseWin: function() {
        if (!this.currentId) {
            alert('钥匙标识丢失记录！');
            return;
        }
        var _this = this;
        if (!this.keyUseWin) {
            this.keyUseWin = new Chenxin.UI.KeyUseWinForm({
                currentId: this.currentId,
                title: '钥匙管理',
                scope: this,
                alwaysCheckPermit: true,
                afterGetFormJson: function(json) {
                    if (this.keyUseWin.op == 'addkeyuse') {

                        json.KeyReceiptionId = this.currentId;
                        this.keyUseWin.additionalParam.keyUserCode = json.keyUserCode;

                        json.HouseAddPerson = json.AddPerson;
                        json.HouseAddress = json.Address;
                        json.HouseDepart = json.AddDepart;
                        json.keyUserDepartment = json.keyUserCodeDepartment;

                        delete json.AddPerson;
                        delete json.Address;
                        delete json.AddDepart;
                        delete json.keyUser;
                        delete json.keyUserCodeDepartment;
                    } else {
                        Chenxin.apply(json, this.currentModel);
                        this.keyUseWin.additionalParam = {};
                    }
                }
            });
        }

        _this.keyUseWin.setEnable();
        this.keyUseWin.op = "addkeyuse";


        if(this.keyUseWin.show(this.keyUseWin.op, { keyid: _this.currentId })) {
            var _data = {};
            Chenxin.apply(_data, this.keyuseinfo);
            _data.Remarks = '';
            this.keyUseWin.fitData(_data);
            this.keyUseWin.currentId = this.currentId;
            this.keyUseWin.setTitle('钥匙使用信息添加');
        }

    },
    showKeyUseWinInfo: function(id, flg) {
        if (!id) {
            alert('钥匙标识丢失记录！');
            return;
        }
        var _this = this;
        if (!this.keyUseWinInfo) {
            this.keyUseWinInfo = new Chenxin.UI.KeyUseInfoWinForm({
                currentId: this.id,
                title: '',
                scope: this,
                alwaysCheckPermit: true,
                afterGetFormJson: function(json) {
                    if (this.keyUseWinInfo.op == 'editkeyuse') {
                        json.KeyId = id;
                        //json.KeyReceiptionId = this.currentId;
                        this.keyUseWinInfo.additionalParam.keyUserCode = json.keyUserCode;

                        json.HouseAddPerson = json.AddPerson;
                        json.HouseAddress = json.Address;
                        json.HouseDepart = json.AddDepart;
                        json.keyUserDepartment = json.keyUserCodeDepartment;

                        delete json.AddPerson;
                        delete json.Address;
                        delete json.AddDepart;
                        delete json.keyUser;
                        delete json.keyUserCodeDepartment;
                    } else {
                        Chenxin.apply(json, this.currentModel);
                        this.keyUseWinInfo.additionalParam = {};
                    }
                }
            });
        }
        var title = "";
        if (flg) {
            title = '钥匙使用信息查看';
            _this.keyUseWinInfo.setDisable();
            this.keyUseWinInfo.op = "view";
        } else {
            title = '钥匙使用信息编辑';
            _this.keyUseWinInfo.setEnable();
            this.keyUseWinInfo.op = "editkeyuse";
        }
        if(this.keyUseWinInfo.show(this.keyUseWinInfo.op, { currentId: id })){
            var _data = {};
            Chenxin.apply(_data, this.keyuseinfo);
            _this.loadinfo(id);
            _data.AddPerson = _this.keyusemodel.HouseAddPerson;
            _data.Address = _this.keyusemodel.HouseAddress;
            _data.AddDepart = _this.keyusemodel.HouseDepart;
            _data.keyUser = _this.keyusemodel.keyUserName;
            _data.keyUserCodeDepartment = _this.keyusemodel.keyUserDepartment;
            _data.InOrOut = _this.keyusemodel.InOrOut;
            _data.keyUserCode = $.trim(_this.keyusemodel.keyUserCode); //_this.keyusemodel.keyUserCode;
            compare = $.trim(_this.keyusemodel.keyUserCode);
            _data.Remarks = _this.keyusemodel.Remarks;
            var temp = _this.keyusemodel.ActionDay.split(' ');
            _data.ActionDay = temp[0];
            this.keyUseWinInfo.fitData(_data);
            this.keyUseWinInfo.currentId = this.currentId;
            this.keyUseWinInfo.setTitle(title);
        }
    },
    createSearch: function() {
        return new Chenxin.UI.KeyManageSearch({
            scope: this
        });
    },
    initComponent: function() {
        Chenxin.UI.KeyManage.superclass.initComponent.call(this);
    }
});


/**
*查询面板
*/
Chenxin.UI.KeyManageSearch = Chenxin.extend(Chenxin.UI.SearchCommon, {
    id: 'key_search',
	initEvent:function(){
		var _this=this;
		$('#btn_search').click(function(){
			_this.scope.reloadGrid();
		});
	},
	createComboList:function(){
		var tracetype=new Chenxin.UI.Combo({
		    containerId: '#keyStatus',
		        name:'keystatus',
				tbxWidth:40,
				items: comboCommon.keyType
            });
            var combl = new Chenxin.UI.StructComboList();
            var structs = combl.comboList;
            structs.push(tracetype);
            return structs;
	},
	initComponent:function(){
	    Chenxin.UI.KeyManageSearch.superclass.initComponent.call(this);
	}
});

/**
*钥匙管理
*/
Chenxin.UI.KeyUseWinForm = Chenxin.extend(Chenxin.UI.FormCommon, {
    actionUrl: 'service/keyAddHandler.ashx',
    id: 'keyUseWin',
    renderTo: 'jqm_keyUseWin',
    winCls: '',
    initFormValidator: function() {
        var _this = this;
        $.formValidator.initConfig({ validatorgroup: this.id, formid: "form_keyUse", onerror: function(msg) { alert(msg) }, onsuccess: function() { return true; } });
        $("#keyUserCode").formValidator({ validatorgroup: this.id, onshow: "请输入工号", onfocus: "输入工号", oncorrect: "输入正确" }).inputValidator({
            min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "员工号不能为空!" }, onerror: "员工号不能为空!"
        }).ajaxValidator({
            url: "service/keyAddHandler.ashx",
            dataType: "json",
            data: "op=validatecode",
            success: function(data) {
                if (typeof (data) == 'string') {
                    eval('data=' + data);
                }
                if (data.success == 0) {
                    $('#keyUser').val('');
                    $('#keyUserCodeDepartment').val('');
                    return false;
                } else {
                    $('#keyUser').val(data.data.model.UserName);
                    $('#keyUserCodeDepartment').val(data.data.model.DepartName);
                    return true;
                }
            },
            buttons: $("#keyUse_Save"),
            error: function() {
                alert("服务器没有返回数据，可能服务器忙，请重试");
            },
            onerror: "员工号不存在",
            onwait: "正在校验，请稍候..."
        });
        $("#keyUseDay").focus(function() {
            WdatePicker({ oncleared: function() { $(this).blur(); }, onpicked: function() { $(this).blur(); } })
        }).formValidator({ validatorgroup: this.id,
            onshow: "请输入时间！", onfocus: "请输入时间", oncorrect: " "
        }).inputValidator({
            min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "时间不能为空！" }, onerror: "时间不能为空！"
        });
    },
    createForm: function() {
        return formCommon.keyUseForm('keyUse');
    },
    initEvent: function() {
        var _this = this;
        $('#keyUse_Save').live('click', function() {
                _this.save();
        });
        $('#keyUse_Cancle').live('click', function() {
            _this.hide();
        });
    },
    initComponent: function() {
        Chenxin.UI.KeyUseWinForm.superclass.initComponent.call(this);
    },
    createComboList: function() {
        var InOrOut = new Chenxin.UI.Combo({
            name: 'InOrOut',
            containerId: '#InOrOut',
            required: true,
            items: comboCommon.keyUseStatus
        });
        return [InOrOut];
    }
});

/**
*钥匙管理
*/
Chenxin.UI.KeyUseInfoWinForm = Chenxin.extend(Chenxin.UI.FormCommon, {
    actionUrl: 'service/keyAddHandler.ashx',
    id: 'keyUseWinInfo',
    renderTo: 'jqm_keyUseWinInfo',
    winCls: '',
    initFormValidator: function() {
        var _this = this;
        $.formValidator.initConfig({ validatorgroup: this.id, formid: "form_keyUse", onerror: function(msg) { alert(msg) }, onsuccess: function() { return true; } });
        $("#keyUserCode").formValidator({ validatorgroup: this.id, onshow: "请输入工号", onfocus: "输入工号", oncorrect: "输入正确" }).inputValidator({
            min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "员工号不能为空!" }, onerror: "员工号不能为空!"
        }).ajaxValidator({
            url: "service/keyAddHandler.ashx",
            dataType: "json",
            data: "op=validatecode",
            success: function(data) {
                if (typeof (data) == 'string') {
                    eval('data=' + data);
                }
                if (data.success == 0) {
                    $('#keyUser').val('');
                    $('#keyUserCodeDepartment').val('');
                    return false;
                } else {
                    $('#keyUser').val(data.data.model.UserName);
                    $('#keyUserCodeDepartment').val(data.data.model.DepartName);
                    return true;
                }
            },
            buttons: $("#keyUse_Save"),
            error: function() {
                alert("服务器没有返回数据，可能服务器忙，请重试");
            },
            onerror: "员工号不存在",
            onwait: "正在校验，请稍候..."
        });
        $("#keyUseDay").focus(function() {
            WdatePicker({ oncleared: function() { $(this).blur(); }, onpicked: function() { $(this).blur(); } })
        }).formValidator({ validatorgroup: this.id,
            onshow: "请输入时间！", onfocus: "请输入时间", oncorrect: " "
        }).inputValidator({
            min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "时间不能为空！" }, onerror: "时间不能为空！"
        });
    },
    createForm: function() {
        return formCommon.keyUseForm('keyUseInfo');
    },
    initEvent: function() {
        var _this = this;
        $('#keyUseInfo_Save').live('click', function() {
            _this.save();
        });
        $('#keyUseInfo_Cancle').live('click', function() {
            _this.hide();
        });
    },
    formValidator: function(model) {
        var _this = this;
        var status = true;
        if (this.op != 'addkeyuse') {
            if (model.keyUserCode == compare) {
                $("#keyUserCode").unFormValidator(true);
            }
        }
        status = jQuery.formValidator.pageIsValid(this.id);
        $("#keyUserCode").unFormValidator(false);
        return status;
    },
    initComponent: function() {
        Chenxin.UI.KeyUseInfoWinForm.superclass.initComponent.call(this);
    },
    createComboList: function() {
        var InOrOut = new Chenxin.UI.Combo({
            name: 'InOrOut',
            containerId: '#InOrOut',
            required: true,
            items: comboCommon.keyUseStatus
        });
        return [InOrOut];
    }
});

$(document).ready(function(){
    var keyMgr = new Chenxin.UI.KeyManage();	
});





